*************************************************************
********** Table A18 Balance Check for Matching *************
*************************************************************

use "$data/Non_payment_dataset_ready.dta" , clear




************************************
** Matching as in tables 5 and a17
************************************

** Entropy Balancing **

gen ebalancematch=0 if itt==.
replace ebalancematch=1 if itt==0

**** Ebalance matching ****
ebalance ebalancematch meanasinhinvoice inactive prenoconsumption paidcount1 paidcount2 meanaveragepayment1 meanaveragepayment2 preclosingbalancew preage_of_accountw presumpaymentratiow if t==12, target(2)
bysort customer: egen entropy_weight=mean(_webal)




** CEM Matching **
set seed 3125

preserve
gen treatmenttomatch=0 if itt==.
replace treatmenttomatch=1 if itt==0
drop if treatmenttomatch==. 
drop if t>12
collapse (mean) meanasinhinvoice inactive prenoconsumption paidcount1 paidcount2 meanaveragepayment1 meanaveragepayment2 preclosingbalancew  presumpaymentratiow  preage_of_accountw if t<209, by(customer treatmenttomatch)
cem  meanasinhinvoice (#5) inactive (#5) prenoconsumption(#5) paidcount1 (#5) paidcount2 (#5) meanaveragepayment1(#5) meanaveragepayment2(#5) preclosingbalancew(#5)  presumpaymentratiow(#5) preage_of_accountw(#5), treatment(treatmenttomatch) k2k 
bysort treatmenttomatch: tabstat meanasinhinvoice inactive prenoconsumption paidcount1 paidcount2 meanaveragepayment1 meanaveragepayment2 preclosingbalancew  presumpaymentratiow preage_of_accountw [aweight=cem_weights] if cem_matched==1, by(treatmenttomatch) s(me v) nototal

keep if cem_matched==1
gen matchedcontrol=1

keep customer matchedcontrol cem_weights
save "$filepath/temp/matchedcontrol", replace // generates a new_datafile with the matching weights
restore

** merge in matched control group *
merge m:1 customer using "$filepath/temp/matchedcontrol", keepusing(matchedcontrol cem_weights) nogen update
bysort customer: egen matchedcontrol2=mean(matchedcontrol)
replace matchedcontrol=matchedcontrol2
drop matchedcontrol2

gen cem_match=0 if matchedcontrol==1 & itt==.
replace cem_match=1 if itt==0 & matchedcontrol==1







*************************************
*** Checking for differences here ***
*************************************


log using "$filepath/output_logs/table A18", replace

 
** Balance Entropy **
preserve
keep if ebalancematch==0 | ebalancematch==1
drop if t>12
sum t
replace t=t-6.721108 // demean time var


reg paidcount c.t##ebalancematch [pweight=entropy_weight], vce(cluster customer)

foreach var of varlist lnpayment asinhpayment asinhinvoice closingbalancew {
reg `var' c.t##ebalancematch [pweight=entropy_weight], vce(cluster customer)
}
restore

	
** Balance CEM **
preserve
drop if cem_match==.
drop if t>12
sum t
replace t=t-6.669847 // demean time var

foreach var of varlist paidcount lnpayment asinhpayment asinhinvoice closingbalancew {
reg `var' c.t##cem_match , vce(cluster customer)
}
restore
	
log close
